package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.MoveBill;
import cn.wolfcode.crm.domain.Officialstu;
import cn.wolfcode.crm.domain.PotentialCustomer;
import cn.wolfcode.crm.mapper.*;
import cn.wolfcode.crm.query.PotentialCustomerQueryObject;
import cn.wolfcode.crm.query.QueryForObject;
import cn.wolfcode.crm.service.IPotentialCustomerService;
import cn.wolfcode.crm.util.BecameDataLoad;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PotentialCustomerServiceImpl implements IPotentialCustomerService {

    @Autowired
    private PotentialCustomerMapper potentialCustomerMapper;
    @Autowired
    private MoveBillMapper moveBillMapper;
    @Autowired
    private OfficialstuMapper officialstuMapper;
    @Autowired
    private ExaminationMapper examinationMapper;
    @Autowired
    private FollowStudentMapper followStudentMapper;

    public void deleteByPrimaryKey(Long id) {
        //删除考试管理信息
        examinationMapper.deleteByPotentialCustomerId(id);
        //删除学员跟踪记录
        followStudentMapper.deleteByPotentialCustomerId(id);
        potentialCustomerMapper.deleteByPrimaryKey(id);
    }

    public void insert(PotentialCustomer potentialCustomer) {
        //保存录入人
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        potentialCustomer.setInputPerson(currentUser.getUsername()+":"+currentUser.getRealname());
        //录入时间
        potentialCustomer.setInputTime(new Date());
        //设置初始状态值
        potentialCustomer.setState(1);
        potentialCustomerMapper.insert(potentialCustomer);
        //保存移交记录
        saveMoveBill(potentialCustomer,"新客户",
                potentialCustomer.getMarketingPerson().getUsername()+":"+potentialCustomer.getMarketingPerson().getRealname());

    }

    public PotentialCustomer selectByPrimaryKey(Long id) {
        return potentialCustomerMapper.selectByPrimaryKey(id);
    }

    public List<PotentialCustomer> selectAll() {
        return potentialCustomerMapper.selectAll();
    }

    public void updateByPrimaryKey(PotentialCustomer potentialCustomer) {
        potentialCustomerMapper.updateByPrimaryKey(potentialCustomer);
    }

    public PageResult queryListByQo(QueryForObject qo) {
        int total = potentialCustomerMapper.getTotalByQo(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<PotentialCustomer> rows = potentialCustomerMapper.getListByQo(qo);
        return new PageResult(total, rows);
    }

    //改变状态
    public void updateStateById(PotentialCustomer potentialCustomer) {
        //如果是放入资源池操作
        if (potentialCustomer.getState()==3){
            potentialCustomer.setResourcePoolTime(new Date());
            //保存移交记录
            //查询该用户信息
            PotentialCustomer currentUser = selectByPrimaryKey(potentialCustomer.getId());
            saveMoveBill(potentialCustomer,currentUser.getMarketingPerson().getUsername()+":"+currentUser.getMarketingPerson().getRealname(),
                    "资源池");
        }
        potentialCustomerMapper.updateStateById(potentialCustomer);
    }

    public PageResult queryResourcePoolListByQo(PotentialCustomerQueryObject qo) {
        int total = potentialCustomerMapper.getyResourcePoolTotalByQo(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<PotentialCustomer> rows = potentialCustomerMapper.getyResourcePoolListByQo(qo);
        return new PageResult(total, rows);
    }

    public void updateMarketingPersonAndStateById(PotentialCustomer potentialCustomer) {
        //判断marketingPerson是否为空
        if(potentialCustomer.getMarketingPerson()==null){
            //为空则存入当前登录用户
            Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
            potentialCustomer.setMarketingPerson(currentUser);
            //保存移交记录
            saveMoveBill(potentialCustomer,"资源池",
                    currentUser.getUsername()+":"+currentUser.getRealname());
        }else {
            //查询该用户信息
            PotentialCustomer currentUser = selectByPrimaryKey(potentialCustomer.getId());
            saveMoveBill(potentialCustomer,"资源池",
                    currentUser.getMarketingPerson().getUsername()+":"+currentUser.getMarketingPerson().getRealname());
        }
        //改变状态值
        potentialCustomer.setState(1);
        potentialCustomerMapper.updateMarketingPersonAndStateById(potentialCustomer);
    }

    //根据学校Id查询该学校有多少潜在学员
    public int queryPotentialCustomerNumberBySchoolId(Long id) {
        return potentialCustomerMapper.queryPotentialCustomerNumberBySchoolId(id);
    }

    //转正操作
    public void become(PotentialCustomer potentialCustome, BecameDataLoad becameDataLoad) {
        //新建正式学员对象
        Officialstu officialstu = new Officialstu();
        //封装数据
        officialstu.setName(potentialCustome.getName());
        officialstu.setAge(potentialCustome.getAge());
        officialstu.setMan( "男".equals(potentialCustome.getGender().trim()));
        officialstu.setTel(potentialCustome.getPhone());
        officialstu.setQqNum(potentialCustome.getQq());
        officialstu.setEmail(potentialCustome.getEmail());
        officialstu.setSchool(potentialCustome.getSchool());
        officialstu.setSchoolId(potentialCustome.getSchoolId().getId());
        officialstu.setEducationId(potentialCustome.getEducationBackground().getId());
        officialstu.setAddress(becameDataLoad.getAddress());
        officialstu.setSourceId(potentialCustome.getSource().getId());
        officialstu.setClientType(becameDataLoad.getClientType().getId().intValue());
        officialstu.setWorkingYear(new Integer(potentialCustome.getJobTime()));
        officialstu.setIttclass(becameDataLoad.getIttclass());
        officialstu.setMarketer(potentialCustome.getMarketingPerson());
        officialstu.setCampusId(potentialCustome.getIntentionCampus().getId());
        officialstu.setPositiveTime(new Date());
        officialstu.setIdCard(becameDataLoad.getIdCard());
        officialstu.setEmergencyName(becameDataLoad.getEmergencyName());
        officialstu.setEmergencyTel(becameDataLoad.getEmergencyTel());
        officialstu.setJobintentionId(becameDataLoad.getJobintentionId());
        officialstu.setState(1);
        officialstu.setPayment(becameDataLoad.getPayment());
        officialstu.setTotalFee(becameDataLoad.getTotalFee());
        officialstu.setPaidFee(becameDataLoad.getPaidFee());
        officialstu.setNoPay(becameDataLoad.getNoPay());
        officialstu.setFeeState(becameDataLoad.getFeeState());
        //保存正式成员
        officialstuMapper.insert(officialstu);
        //更改潜在客户信息
        //设置状态为正式学员
        potentialCustome.setState(4);
        potentialCustomerMapper.become(potentialCustome);
        //保存移交记录
        //保存移交记录
        //查询该用户信息
        PotentialCustomer currentUser = selectByPrimaryKey(potentialCustome.getId());
        saveMoveBill(potentialCustome,currentUser.getMarketingPerson().getUsername()+":"+currentUser.getMarketingPerson().getRealname(),
                "正式学员");
    }

    //保存移交记录
    private void saveMoveBill(PotentialCustomer potentialCustomer,String formerPerson,String moveTarget){
        potentialCustomer = selectByPrimaryKey(potentialCustomer.getId());
        MoveBill bill = new MoveBill();
        bill.setName(potentialCustomer.getName());
        bill.setPhone(potentialCustomer.getPhone());
        bill.setQq(potentialCustomer.getQq());
        bill.setCreateTime(potentialCustomer.getCreateTime());
        bill.setMoveTime(new Date());
        bill.setFormerPerson(formerPerson);
        bill.setMoveTarget(moveTarget);
        moveBillMapper.insert(bill);
    }
}
